package com.qdu.controller;

import com.alibaba.druid.util.StringUtils;
import com.qdu.entity.*;
import com.qdu.service.*;
import com.qdu.util.GenerateUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
// springboot自带的解决跨域问题的注解
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    UserService userService;

    @Autowired
    HouseService houseService;

    @Autowired
    ExamineService examineService;

    @Autowired
    ReportService reportService;


    @PostMapping("/addOrUpdate")
    public Result addAdmin(@RequestBody Admin admin){
        System.out.println("第一次打印"+admin);
        Result result = new Result();
        try{
            if(StringUtils.isEmpty(admin.getAid())){
                // 新建管理员
                // 管理员重名的处理
                if(adminService.getOneByName(admin.getAname()) != null){
                    result.setStatus(false);
                    result.setMsg("重名，请修改·管理员姓名后重新添加");
                }else{
                    admin.setAid("A" + GenerateUUID.generateUUID());
                    adminService.addAdmin(admin);
                    System.out.println("第二次打印"+admin);
                    result.setMsg("用户信息保存成功");
                }
            }else{
                // 更新管理员信息
                adminService.updateAdmin(admin);
                result.setMsg("更新成功");
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            result.setStatus(false);
            result.setMsg("系统错误：保存用户失败，请稍后再试");
        }
        return result;
    }

    @GetMapping("/delete")
    public Result deleteAdmin(String aid){
        Result result = new Result();
        try {
            adminService.deleteAdmin(aid);
            result.setMsg("删除用户信息成功");
        }catch (Exception ex){
            result.setStatus(false);
            result.setMsg("删除失败，请稍后再试");
        }
        return result;
    }

    // 分页查询的方法
    @GetMapping("/findByPage")
    public Map<String,Object> findByPage(Integer pageNow, Integer pageSize){
//        System.out.println("第一次打印" + pageNow + "+" + pageSize);
        Map<String, Object> result = new HashMap<String, Object>();
        pageNow = pageNow == null ? 0 : pageNow;    // 从第0个开始处理，默认为第零个
        pageSize = pageSize == null ? 5 : pageSize;// 默认为每页显示五个
//        System.out.println("第二次打印" + pageNow + "+" + pageSize);
        List<Admin> admins = adminService.getAdminByPage(pageNow, pageSize);
//        System.out.println(admins);

        Long totals = adminService.findTotals();
        result.put("admins", admins);
        result.put("total",totals);
        return result;
    }

    // 用户信息的分页
    @GetMapping("/findUserByPage")
    public Map<String, Object> getUserBypage(Integer pageNow, Integer pageSize){
        HashMap<String, Object> result = new HashMap<>();
        pageNow = pageNow == null ? 0 : pageNow;    // 从第0个开始处理，默认为第零个
        pageSize = pageSize == null ? 5 : pageSize;// 默认为每页显示五个
        List<Userinfo> users = userService.getUserByPage(pageNow, pageSize);
        Long totals = userService.findTotals();

        result.put("users", users);
        result.put("total", totals);
        return result;
    }

    // 房屋信息的分页
    @GetMapping("/findHouseByPage")
    public Map<String, Object> getHouseBypage(Integer pageNow, Integer pageSize){
        HashMap<String, Object> result = new HashMap<>();
        pageNow = pageNow == null ? 0 : pageNow;    // 从第0个开始处理，默认为第零个
        pageSize = pageSize == null ? 5 : pageSize;// 默认为每页显示五个
        List<Houseinfo> houses = houseService.getHouseByPage(pageNow, pageSize);
        Long totals = houseService.findTotals();

        result.put("houses", houses);
        result.put("total", totals);
        return result;
    }

    @PostMapping("/updateHouse")
    public Result updateHouse(@RequestBody Houseinfo house){
        System.out.println("第一次打印"+house);
        Result result = new Result();
        try{
            if(house != null){
                Examineinfo examineinfo = new Examineinfo();
                houseService.updateByPrimaryKey(house);
                result.setMsg("房屋信息修改成功");
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            result.setStatus(false);
            result.setMsg("系统错误：修改信息失败，请稍后再试");
        }
        return result;
    }


    // 审核表单
    @GetMapping("/findExamineByPage")
    public Map<String, Object> getExamineBypage(Integer pageNow, Integer pageSize){
        HashMap<String, Object> result = new HashMap<>();
        List<Map> infos = new ArrayList<>();

        pageNow = pageNow == null ? 0 : pageNow;    // 从第0个开始处理，默认为第零个
        pageSize = pageSize == null ? 5 : pageSize;// 默认为每页显示五个

        List<Examineinfo> examines = examineService.getExamineByPage(pageNow, pageSize);

        for(Examineinfo examineinfo : examines){
            HashMap<String, Object> houseInfo = new HashMap<>();
            Userinfo userinfo = userService.selectByPrimaryKey(examineinfo.getUid());
            Houseinfo houseinfo = houseService.selectByHid(examineinfo.getHid());
            Admin admin = adminService.getOneByAid(examineinfo.getAid());
            houseInfo.put("user", userinfo);
            houseInfo.put("house", houseinfo);
            houseInfo.put("admin", admin);
            houseInfo.put("examine", examineinfo);
            infos.add(houseInfo);
        }

        Long totals = examineService.findTotals();

        result.put("examines", infos);
        result.put("total", totals);
        return result;
    }

    @PostMapping("/updateExamine")
    public Result updateExamine(@RequestBody Map<String, Object> examine){
        System.out.println("第一次打印"+examine);
        Result result = new Result();
        String aid = "";
        List<Admin> admins = adminService.getAll();
        for(Admin admin : admins){
            if(examine.get("aid").equals(admin.getAname())){
                aid = admin.getAid();
                break;
            }
        }
        System.out.println(aid);
        try{
            if(examine != null){
                Examineinfo examineinfo = new Examineinfo();
                examineinfo.setUid(examine.get("uid").toString());
                examineinfo.setHid(examine.get("hid").toString());
                examineinfo.setAid(aid);
                examineinfo.setState(Integer.parseInt(examine.get("state").toString()));
                examineService.update(examineinfo);
                result.setMsg("房屋审核信息修改成功");
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            result.setStatus(false);
            result.setMsg("系统错误：修改信息失败，请稍后再试");
        }
        return result;
    }

    @GetMapping("/findReportByPage")
    public Map<String, Object> getReportBypage(Integer pageNow, Integer pageSize){
        HashMap<String, Object> result = new HashMap<>();
        List<Map> infos = new ArrayList<>();

        pageNow = pageNow == null ? 0 : pageNow;    // 从第0个开始处理，默认为第零个
        pageSize = pageSize == null ? 5 : pageSize;// 默认为每页显示五个

        List<Reportinfo> reports = reportService.getAll();

        for(Reportinfo report : reports){
            HashMap<String, Object> reportInfo = new HashMap<>();
            Userinfo userinfo = userService.selectByPrimaryKey(report.getUid());
            Houseinfo houseinfo = houseService.selectByHid(report.getHid());
            Admin admin = adminService.getOneByAid(report.getAid());
            reportInfo.put("user", userinfo);
            reportInfo.put("house", houseinfo);
            reportInfo.put("admin", admin);
            reportInfo.put("report", report);
            infos.add(reportInfo);
        }

        Long totals = reportService.findTotals();

        result.put("reports", infos);
        result.put("total", totals);
        return result;
    }

    @PostMapping("/updateReport")
    public Result updateReport(@RequestBody Map<String, Object> report){
        System.out.println("第一次打印"+report);
        List<Admin> admins = adminService.getAll();
        String aid = "";
        for(Admin admin : admins){
            if(admin.getAname().equals(report.get("aid"))){
                aid = admin.getAid();
                break;
            }
        }
        System.out.println("aid" + aid);
        Result result = new Result();
        try{
            if(report != null){
                Reportinfo reportinfo = new Reportinfo();
                reportinfo.setUid(report.get("uid").toString());
                reportinfo.setHid(report.get("hid").toString());
                reportinfo.setAid(aid);
                reportinfo.setRid(report.get("rid").toString());
                reportinfo.setReason(report.get("reason").toString());
                reportinfo.setState(Integer.parseInt(report.get("state").toString()));
                reportService.updateByPrimaryKey(reportinfo);
                result.setMsg("举报信息状态修改成功");
            }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            result.setStatus(false);
            result.setMsg("系统错误：修改失败，请稍后再试");
        }
        return result;
    }


}
